<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <title>设备联动</title>
    <link rel="stylesheet" href="../css/header.css">
    <link rel="stylesheet" href="../css/layout.css">
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/mqttws31.js"></script>
    <script type="text/javascript" src="../script/jquery-2.1.3.js"></script>
    <script type="text/javascript" src="../script/mico_speech.js"></script>
</head>
<body>
    <div class="wrapper wuhuakongzhiqi"> 
        <!-- header -->
        <header id="header" class="header">
            <div class="head">
                <a class="left" onclick="checkpage()" id="backleft"></a>
                <span class="center" id="device_name">雾化控制器</span>
                <a class="right" onclick="setting()" id="backright"></a>
            </div>
        </header>
        <!-- content -->
        <div class="content">
            <div class="block pressval clearfix">
                <div class="wrap">
                    <span class="val">40</span>
                    <span class="unittxt">KG</span>
                    <div class="presstxt">压力值大小</div>
                </div>
            </div>
            <div class="block status clearfix">
                <div class="col_50">
                    <div class="devstatus">ON</div>
                    <div class="devtxt">运行状态</div>
                </div>
                <div class="col_50">
                    <img class="commustatus" src="../image/errorinfo.svg">
                    <div class="commutxt">通讯故障</div>
                </div>
            </div>
            <div class="block systimedisp clearfix">
                <div class="col_50">
                    <img class="timepic" src="../image/systime.svg">
                    <span class="timetxt">时间</span>
                </div>
                <div class="col_50">
                    <span class="timeval">00:00:00</span>
                </div>
            </div>
            <div class="block devmode clearfix">
                <div class="col_50">
                    <img class="modepic" src="../image/devmode.svg">
                    <span class="modetxt">模式</span>
                </div>
                <div class="col_50">
                    <span class="modeval">自动连续</span>
                </div>
            </div>
            <div class="block devstatusbtn clearfix">
                <div class="col_50"><div class="btn">停止</div></div>
                <div class="col_50"><div class="btn">运行</div></div>
            </div>
            <br/>
            <div id="device_id"></div>
            <div id="debug"></div>
        </div>
    </div>
</body>
    <script>
        var device_id = new Array();
        var device_name = new Array();

        var systimerecv;     //系统时间

        var lighttimerecv;     //背光时间

        var lightlevelrecv;     //灯光亮度

        var ctlmoderecv;     //控制方式

        //var inputiorecv;     //输入端口
        var input0recv;
        var input1recv;
        var input2recv;
        var input3recv;
        var ain0recv;
        var ain1recv;
        var ain3recv;

        //var outputiorecv;     //输出端口
        var pout0recv;
        var pout1recv;
        var out0recv;
        var out1recv;
        var out2recv;
        var out3recv;
        var out4recv;

        var fengintmoderecv;     //风间隙模式
        var wuintmoderecv;     //雾间隙模式

        var openinttimerecv;     //间隙开时间
        var closeinttimerecv;     //间隙关时间

        var fengtimswitchrecv;     //风定时
        var wutimswitchrecv;     //雾定时
        var dengtimswitchrecv;     //灯定时

        var timtime_fengkairecv;     //风定时开
        var timtime_fengguanrecv;     //风定时关
        var timtime_wukairecv;     //雾定时开
        var timtime_wuguanrecv;     //雾定时关
        var timtime_dengkairecv;     //灯定时开
        var timtime_dengguanrecv;     //灯定时关

        var funcselswitchrecv;     //功能开关

        var shangxianpressrecv;     //压力上限
        var xiaxianpressrecv;     //压力下限
        var shedingpressrecv;     //压力设定
        var liangchengpressrecv;     //压力量程

        var micoMqtt = null;
        //听我指令
        var listenme = 0;
        apiready = function() {
            if (parseFloat(api.systemVersion.substring(0,3)) >= 4.4) {
                $(".header").css("padding-top", "35px");
                $(".content").css("height", document.body.clientHeight - 76);
            } else {
                $(".header").css("padding-top", "15px");
                $(".content").css("height", document.body.clientHeight - 56);
            }
            device_id = api.pageParam.deviceid;
            device_name = api.pageParam.devicename;
            // document.getElementById('device_name').innerHTML = api.pageParam.devicename;
            // 如果设备ID不为空，则执行连接MQTT的操作
            if (device_id !== null) {
                $(device_id).(function(n,element){
                    ez_connect(element);//连接每一个
                });
            }
            if (api.systemType != 'ios') {
                api.addEventListener({
                    name : "keyback"
                }, function(ret, err) {
                    checkpage();
                });
            } else {
                $(".header").css("padding-top", "30px");
                $(".left").css("top", "32px");
            }
        };
        // 连接MQTT服务
        function ez_connect(device_id) {
            //document.getElementById('device_id').innerHTML = "设备ID：" + device_id;
            //首先引用MiCOmqtt
            micoMqtt = api.require("micoMqtt2");
            var host = "api.easylink.io";
            var username = "";
            var password = "";
            //clientID，需要按照此标准来定义：v1-app-[MAC]  版本号-app-手机MAC(12位)
            var clientID = "v1-app-" + parseInt(Math.random() * (1000000000000), 12);
            var topic = device_id + '/out/#';
            micoMqtt.startMqtt({
                micoMqtt : micoMqtt,
                host : host,
                username : username,
                password : password,
                clientID : clientID,
                topic : topic
            }, function(ret, err) {
                if (ret.status) {
                    var commuinfo = '<div class="commustatus">正常</div>' 
                        + '<div class="commutxt">' 
                        + '通讯状态'
                        + '</div>';
                    $(".status>.col_50:last-child").html(commuinfo);
                    $(".status>.col_50:last-child>.commutxt").css("color","#9367b6"); 
                    console.log("连接成功");
                    recvmsg();
                } else {
                    var commuinfo = '<img class="commustatus" src="../image/errorinfo.svg">' 
                        + '<div class="commutxt">' 
                        + '通讯故障'
                        + '</div>';
                    $(".status>.col_50:last-child").html(commuinfo);

                    console.log("连接失败");
                }
            });
        }
        
        //停止按钮点击事件
        $(".devstatusbtn .col_50:first-child .btn").click(function() {
            var topic = device_id + '/in';
            var command;
            command = '{"devstatus":false}';     //开关命令
            publishcmd(topic, command);
        });
        //运行按钮点击事件
        $(".devstatusbtn .col_50:last-child .btn").click(function() {
            var topic = device_id + '/in';
            var command;
            command = '{"devstatus":true}';
            publishcmd(topic, command);
        });

        //发送指令
        function publishcmd(topic, command) {
            micoMqtt.publish({
                topic : topic,
                command : command
            }, function(ret, err) {
            });
        }

        var settingctrl = false;    //设置页面需要设备处于停止状态才可以配置
        // 接收数据
        function recvmsg() {
            var msgindex = 1;
            var msgjson;
            micoMqtt.recvMqttMsg(function(ret, err) {
                //              alert(JSON.stringify(ret));
                if (ret) {
                    if (msgindex > 12) {
                        msgindex = 1;
                        document.getElementById('debug').innerHTML = "";
                    }
                    msgindex++;
                    msgjson = ret.subs;
                    var devpressval = msgjson.dev_pressval/10;     //设备压力之大小
                    var devstatus = msgjson.dev_status;         //设备运行状态
                    if(devstatus == 0){
                        settingctrl = true;
                    }else{
                        settingctrl = false;
                    }

                    var devtime = msgjson.dev_time;             //设备时间
                    var devtimeshi = Math.floor(devtime/10000);//msgjson.dev_time_shi;      
                    var devtimefen = Math.floor(devtime%10000/100);//msgjson.dev_time_fen;
                    var devtimemiao = Math.floor(devtime%100);//msgjson.dev_time_miao;

                    var devmode = msgjson.dev_mode;             //设备模式
//////////////////////////////////////////////////////////////////////////////////////////////////////

                    systimerecv = msgjson.systime;//系统时间

                    lighttimerecv = msgjson.lighttime;//背光时间

                    lightlevelrecv = msgjson.lightlevel;//灯光亮度

                    ctlmoderecv = msgjson.ctlmode;//控制方式

                    // inputiorecv = msgjson.inputio;//输入端口
                    // outputiorecv = msgjson.outputio;//输出端口

                    input0recv = msgjson.input0;
                    input1recv = msgjson.input1;
                    input2recv = msgjson.input2;
                    input3recv = msgjson.input3;
                    ain0recv = msgjson.ain0;
                    ain1recv = msgjson.ain1;
                    ain3recv = msgjson.ain3;


                    pout0recv = msgjson.pout0;
                    pout1recv = msgjson.pout1;
                    out0recv = msgjson.out0;
                    out1recv = msgjson.out1;
                    out2recv = msgjson.out2;
                    out3recv = msgjson.out3;
                    out4recv = msgjson.out4;

                    fengintmoderecv = msgjson.fengintmode;//风间隙模式
                    wuintmoderecv = msgjson.wuintmode;//雾间隙模式

                    openinttimerecv = msgjson.openinttime;//间隙开时间
                    closeinttimerecv = msgjson.closeinttime;//间隙关时间

                    fengtimswitchrecv = msgjson.fengtimswitch;//风定时
                    wutimswitchrecv = msgjson.wutimswitch;//雾定时
                    dengtimswitchrecv = msgjson.dengtimswitch;//灯定时

                    timtime_fengkairecv = msgjson.timtime_fengkai;//风定时开
                    timtime_fengguanrecv = msgjson.timtime_fengguan;//风定时关
                    timtime_wukairecv = msgjson.timtime_wukai;//雾定时开
                    timtime_wuguanrecv = msgjson.timtime_wuguan;//雾定时关
                    timtime_dengkairecv = msgjson.timtime_dengkai;//灯定时开
                    timtime_dengguanrecv = msgjson.timtime_dengguan;//灯定时关

                    funcselswitchrecv = msgjson.funcselswitch;//功能开关

                    shangxianpressrecv = msgjson.shangxianpress;//压力上限
                    xiaxianpressrecv = msgjson.xiaxianpress;//压力下限
                    shedingpressrecv = msgjson.shedingpress;//压力设定
                    liangchengpressrecv = msgjson.liangchengpress;//压力量程

//////////////////////////////////////////////////////////////////////////////////////////////////////
                    if ("undefined" != typeof (devpressval) ||
                    "undefined" != typeof (devstatus) || 
                    "undefined" != typeof (devtime) || 
                    "undefined" != typeof (devmode)) {
                        // 显示设备压力大小
                        $(".pressval>.wrap>.val").text(devpressval);
                        // 显示运行状态
                        switch(devstatus){
                            case 0:$(".status>.col_50>.devstatus").text("OFF");
                                $(".status>.col_50>.devstatus").css("color","#f93a71"); 
                                $(".status>.col_50>.devtxt").css("color","#f93a71"); 
                                break;
                            case 1:$(".status>.col_50>.devstatus").text("ON");
                                $(".status>.col_50>.devstatus").css("color","#9367b6"); 
                                $(".status>.col_50>.devtxt").css("color","#9367b6"); 
                                break;
                            case 2:$(".status>.col_50>.devstatus").text("缺水");
                                $(".status>.col_50>.devstatus").css("color","#f93a71"); 
                                $(".status>.col_50>.devtxt").css("color","#f93a71"); 
                                break;
                            case 3:$(".status>.col_50>.devstatus").text("欠压");
                                $(".status>.col_50>.devstatus").css("color","#f93a71"); 
                                $(".status>.col_50>.devtxt").css("color","#f93a71"); 
                                break;
                        }
                        
                        //设备时间
                        devtimeshi = ("0" + (devtimeshi*1)).substring(("0" + (devtimeshi*1)).length-2,("0" + (devtimeshi*1)).length);
                        devtimefen = ("0" + (devtimefen*1)).substring(("0" + (devtimefen*1)).length-2,("0" + (devtimefen*1)).length);
                        devtimemiao = ("0" + (devtimemiao*1)).substring(("0" + (devtimemiao*1)).length-2,("0" + (devtimemiao*1)).length);
                        $(".systimedisp>.col_50:last-child>.timeval").text(devtimeshi+":"+devtimefen+":"+devtimemiao);
                        //设备模式
                        if(ctlmoderecv == 1){
                            if(fengintmoderecv || wuintmoderecv){
                                devmode = 3;//手动间隙
                            }else{
                                devmode = 2;//手动连续
                            }
                        }else{
                            if(fengintmoderecv || wuintmoderecv){
                                devmode = 1;//自动间隙
                            }else{
                                devmode = 0;//自动连续
                            }
                        }
                        switch(devmode){
                            case 0: $(".devmode>.col_50:last-child>.modeval").text("自动连续"); break;
                            case 1: $(".devmode>.col_50:last-child>.modeval").text("自动间隙"); break;
                            case 2: $(".devmode>.col_50:last-child>.modeval").text("手动连续"); break;
                            case 3: $(".devmode>.col_50:last-child>.modeval").text("手动间隙"); break;
                            default: break;
                        }
                    }
                } else {
                    api.alert({
                        msg : err.msg
                    });
                }
            });
        }

        // 日志打印在页面的部分
        var i = 0;
        console.log = ( function(old_funct, div_log) {
                return function(text) {
                    old_funct(text);
                    var p = '';
                    if (i % 2 == 0)
                        p = '<p>';
                    else
                        p = '<p class=\'gray\'>';
                    if ( typeof text === "object")
                        div_log.innerHTML += p + JSON.stringify(text) + '</p>';
                    else
                        div_log.innerHTML += p + text + '</p>';
                    div_log.scrollTop = div_log.scrollHeight;
                    i += 1;
                };
            }(console.log.bind(console), document.getElementById("debug")));
        console.error = console.debug = console.info = console.log;
        //      返回上一页
        function checkpage() {
            api.closeWin();
            //关闭mqtt，关闭的时候需要同事关闭消息接收和MQTT连接
            micoMqtt.stopRecvMqttMsg(function(ret, err) {
            });
            micoMqtt.stopMqtt(function(ret, err) {
            });
            window.location.href = "./devlist.html";
        };
        // function setting(){
        //     window.location.href = "./wuhuakongzhiqi.html";
        // }
        // 跳转到控制设备的页面，如果设备离线则不让跳转
        function setting() {
            //api.closeWin();
            if(settingctrl){
                api.openWin({
                name : 'wuhuakongzhiqi',
                url : './wuhuakongzhiqi.html',
                slidBackEnabled : false,
                reload : true,
                bgColor : '#F0F0F0',
                animation : {
                    type : 'none'
                },
                pageParam : {
                    "deviceid" : device_id,
                    "devicename" : device_name,
                    "systimerecv" : systimerecv,
                    "lighttimerecv" : lighttimerecv,

                    "systimerecv" : systimerecv,         //系统时间

                    "lighttimerecv" : lighttimerecv,         //背光时间

                    "lightlevelrecv" : lightlevelrecv,         //灯光亮度

                    "ctlmoderecv" : ctlmoderecv,         //控制方式

                    // "inputiorecv" : inputiorecv,         //输入端口
                    // "outputiorecv" : outputiorecv,         //输出端口
                    
                    "input0recv" : input0recv,
                    "input1recv" : input1recv,
                    "input2recv" : input2recv,
                    "input3recv" : input3recv,
                    "ain0recv" : ain0recv,
                    "ain1recv" : ain1recv,
                    "ain3recv" : ain3recv,


                    "pout0recv" : pout0recv,
                    "pout1recv" : pout1recv,
                    "out0recv" : out0recv,
                    "out1recv" : out1recv,
                    "out2recv" : out2recv,
                    "out3recv" : out3recv,
                    "out4recv" : out4recv,


                    "fengintmoderecv" : fengintmoderecv,         //风间隙模式
                    "wuintmoderecv" : wuintmoderecv,         //雾间隙模式

                    "openinttimerecv" : openinttimerecv,         //间隙开时间
                    "closeinttimerecv" : closeinttimerecv,         //间隙关时间

                    "fengtimswitchrecv" : fengtimswitchrecv,         //风定时
                    "wutimswitchrecv" : wutimswitchrecv,         //雾定时
                    "dengtimswitchrecv" : dengtimswitchrecv,         //灯定时

                    "timtime_fengkairecv" : timtime_fengkairecv,         //风定时开
                    "timtime_fengguanrecv" : timtime_fengguanrecv,         //风定时关
                    "timtime_wukairecv" : timtime_wukairecv,         //雾定时开
                    "timtime_wuguanrecv" : timtime_wuguanrecv,         //雾定时关
                    "timtime_dengkairecv" : timtime_dengkairecv,         //灯定时开
                    "timtime_dengguanrecv" : timtime_dengguanrecv,         //灯定时关

                    "funcselswitchrecv" : funcselswitchrecv,         //功能开关

                    "shangxianpressrecv" : shangxianpressrecv,         //压力上限
                    "xiaxianpressrecv" : xiaxianpressrecv,         //压力下限
                    "shedingpressrecv" : shedingpressrecv,         //压力设定
                    "liangchengpressrecv" : liangchengpressrecv         //压力量程
                }
            });
            }else{
                return;
            }
            
            
        }
    </script>
</html>
